package cn.good.payment.controller;

import cn.good.dto.Dto;
import cn.good.dto.DtoUtil;
import cn.good.payment.servcie.GDOrderService;
import cn.good.pojo.Order;
import cn.good.utils.EmptyUtils;
import cn.good.vo.OrderCreateVO;
import cn.good.vo.OrderSearchVO;
import cn.good.vo.PageListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/doc/order")
public class OrderController {

    @Resource
    private GDOrderService gdOrderService;

    @ApiOperation(value="查询订单信息",httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="查询订单信息 ")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET,produces = "application/json")
    public Dto searchOrderById(@PathVariable Long id){

        try {
        Order order = gdOrderService.searchOrderById(id);
        return DtoUtil.returnDataSuccess(order);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","2000001");
        }
    }
    @ApiOperation(value="查询订单列表",httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="查询订单列表 ")
    @RequestMapping(value = "/list",method = RequestMethod.GET,produces = "application/json")
    public Dto searchOrderList(@RequestBody OrderSearchVO orderSearchVO){
        try {
            PageListVO list = gdOrderService.searchOrderList(orderSearchVO);
            return DtoUtil.returnDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","20002");
        }
    }
    @ApiOperation(value="创建订单",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="创建订单 ")
    @RequestMapping(value = "/create",method = RequestMethod.POST,produces = "application/json")
    public Dto createOrder(@RequestBody OrderCreateVO orderCreateVO){
        if(EmptyUtils.isEmpty(orderCreateVO.getUserId())||EmptyUtils.isEmpty(orderCreateVO.getServicePriceId())||EmptyUtils.isEmpty(orderCreateVO.getMakeDate())){
            return DtoUtil.returnFail("参数错误！","20006");
        }
        try {
           Order order = gdOrderService.createOrder(orderCreateVO);
           return DtoUtil.returnSuccess("创建订单成功！",order);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("生成失败！","20008");
        }
    }

}
